<?php
require_once __DIR__."/Base_model.php";
class Host_sticker_model extends Base_model
{
    public function __construct()
    {
        parent::__construct();
        $this->sticker = "sticker";//贴纸表
        $this->sticker_type = "sticker_type";//贴纸分类表
    }

    /**
     * 查询所有贴纸分类和贴纸
     */
    public function all(){
        //1.先去查询有几个贴纸分类
        $this->db->select("*");
        $this->db->from($this->sticker_type);
        $this->db->order_by('time_tmp',"desc");
        $res = $this->db->get();
        $datas= $res->result_array();
        if(!empty($datas)){
            foreach($datas as $k=>&$v){
                $this->db->select("*");
                $this->db->from($this->sticker);
                $this->db->where("sticker_id",$v['id']);
                $this->db->order_by('sorts',"desc");
                $res = $this->db->get();
                $data= $res->result_array();
                $v['children']=$data;
            }
        }
        return $datas;
    }

    /**
     * 查询所有贴纸
     */
    public function sticker($id){
        $this->db->select("*");
        $this->db->from($this->sticker);
        $this->db->where("sticker_id",$id);
        $this->db->order_by('sorts',"desc");
        $res = $this->db->get();
        $datas= $res->result_array();
        return $datas;
    }

    /**
     * 贴纸分类上移
     */
    public function up_sort($start_id,$now_id){
        //1.当前的排序位置
        $now=$this->one($this->sticker_type,['id'=>$now_id],"id,time_tmp");
        //2.这个排序值上一个的排序位置
        $start=$this->one($this->sticker_type,['id'=>$start_id],"id,time_tmp");

        $this->db->trans_begin();// 开启事务
        //3.先把现在的排序位置改为以前的
        $this->db->update($this->sticker_type, ['time_tmp' => $start['time_tmp']],['id'=>$now['id']]);
        $arr = $this->db->affected_rows();//受影响的行数
        //4.再把以前的排序位置改为现在的
        $this->db->update($this->sticker_type, ['time_tmp' => $now['time_tmp']],['id'=>$start['id']]);
        if ($this->db->trans_status() === false || $arr == 0) {
            $this->db->trans_rollback();//回滚
            return false;
        } else {
            $this->db->trans_commit();//提交事物
            return true;
        }
    }

    /**
     * 贴纸分类下移
     */
    public function down_sort($end_id,$now_id){
        //1.当前的排序位置
        $now=$this->one($this->sticker_type,['id'=>$now_id],"id,time_tmp");
        //2.这个排序值上一个的排序位置
        $end=$this->one($this->sticker_type,['id'=>$end_id],"id,time_tmp");

        $this->db->trans_begin();// 开启事务
        //3.先把现在的排序位置改为以前的
        $this->db->update($this->sticker_type, ['time_tmp' => $end['time_tmp']],['id'=>$now['id']]);
        $arr = $this->db->affected_rows();//受影响的行数
        //4.再把以前的排序位置改为现在的
        $this->db->update($this->sticker_type, ['time_tmp' => $now['time_tmp']],['id'=>$end['id']]);
        if ($this->db->trans_status() === false || $arr == 0) {
            $this->db->trans_rollback();//回滚
            return false;
        } else {
            $this->db->trans_commit();//提交事物
            return true;
        }
    }

    /**
     * 贴纸上移
     */
    public function sticker_up($start_id,$now_id){
        //1.当前的排序位置
        $now=$this->one($this->sticker,['id'=>$now_id],"id,sorts");
        //2.这个排序值上一个的排序位置
        $start=$this->one($this->sticker,['id'=>$start_id],"id,sorts");

        $this->db->trans_begin();// 开启事务
        //3.先把现在的排序位置改为以前的
        $this->db->update($this->sticker, ['sorts' => $start['sorts']],['id'=>$now['id']]);
        $arr = $this->db->affected_rows();//受影响的行数
        //4.再把以前的排序位置改为现在的
        $this->db->update($this->sticker, ['sorts' => $now['sorts']],['id'=>$start['id']]);
        if ($this->db->trans_status() === false || $arr == 0) {
            $this->db->trans_rollback();//回滚
            return false;
        } else {
            $this->db->trans_commit();//提交事物
            return true;
        }
    }

    /**
     * 贴纸分类下移
     */
    public function sticker_down($end_id,$now_id){
        //1.当前的排序位置
        $now=$this->one($this->sticker,['id'=>$now_id],"id,sorts");
        //2.这个排序值上一个的排序位置
        $end=$this->one($this->sticker,['id'=>$end_id],"id,sorts");

        $this->db->trans_begin();// 开启事务
        //3.先把现在的排序位置改为以前的
        $this->db->update($this->sticker, ['sorts' => $end['sorts']],['id'=>$now['id']]);
        $arr = $this->db->affected_rows();//受影响的行数
        //4.再把以前的排序位置改为现在的
        $this->db->update($this->sticker, ['sorts' => $now['sorts']],['id'=>$end['id']]);
        if ($this->db->trans_status() === false || $arr == 0) {
            $this->db->trans_rollback();//回滚
            return false;
        } else {
            $this->db->trans_commit();//提交事物
            return true;
        }
    }

    /**
     * 发布贴纸分类
     */
    public function fabu(){
        //1.先去查询出所有的贴纸分类
        $this->db->select("*");
        $this->db->from($this->sticker_type);
        $res = $this->db->get();
        $datas= $res->result_array();
        $da1=[];
        //2.去把后台展示的替换到前端
        $length=count($datas);
        if(!empty($datas)){
            foreach($datas as $k=>$v){
                $da1['sticker_type_name']=$v['test_sticker_type_name'];
                $da1['select_icon']=$v['test_select_icon'];
                $da1['no_select_icon']=$v['test_no_select_icon'];
                $da1['status']=$v['test_status'];
                if($k==$length-1){
                    return $this->db->update($this->sticker_type,$da1,['id'=>$v['id']]);
                }else{
                    $this->db->update($this->sticker_type,$da1,['id'=>$v['id']]);
                }

            }
        }
    }
}